"""
@Project:${2108a 人工智能}
@File:course.py
@Author:周家昌
@Date:14:21
"""

import random
import sys
import datetime



from common.models import db
from common.models.base import Base


class CourseType(Base):
    """
    课程类别
    """
    id = db.Column(db.Integer, primary_key=True, doc='课程类型id')
    title = db.Column(db.String(16), doc='课程类别')
    sequence = db.Column(db.Integer, doc='展示顺序', default=10)
    course = db.relationship('Course', backref=db.backref('type'))

    __tablename__ = 'tb_course_type'

    def __str__(self):
        return self.title


class Tag(Base):
    """
    课程标签
    """
    __tablename__ = 'tb_course_tag'


    id = db.Column(db.Integer, primary_key=True, doc='课程标签id')
    title = db.Column(db.String(16), doc='课程类别')
    sequence = db.Column(db.Integer, doc='展示顺序', default=10)
    course = db.relationship('Course', secondary='course_tag', backref=db.backref('tags'))


    def __str__(self):
        return self.title


class Course(Base):
    """
    课程表
    """
    __tablename__ = 'tb_course'

    STATUS = (
        ('0', '即将上线'),
        ('1', '已上线'),
        ('2', '已下线'),
    )
    id = db.Column(db.Integer, primary_key=True, doc='课程id')
    title = db.Column(db.String(24), doc='课程名称')
    desc = db.Column(db.String(256), doc='课程描述')
    img_path = db.Column(db.String(1000), doc='课程logo地址')
    course_type = db.Column(db.Integer, db.ForeignKey("tb_course_type.id", ondelete="CASCADE"))
    status = db.Column(db.Integer, doc='课程logo地址', default=1)
    follower = db.Column(db.Integer, default=0, doc='关注人数')
    learner = db.Column(db.Integer, default=0, doc='学习人数')
    chapters = db.relationship('Chapters', backref='tb_course')
    comments = db.relationship('Comment',backref='course')
    jieduan = db.Column(db.Integer, db.ForeignKey('jieduan_tb.id',ondelete="CASCADE"))

    # def __str__(self):
    #     return self.id

    def to_json(self):
        """
        自定义格式
        """
        data = {
            'name': self.title,
            'collect_num': self.follower,
            'category': self.course_type
        }
        return data


class CourseTag(Base):
    """
    中间表  课程、标签的中间表
    """
    __tablename__ = 'course_tag'
    course_id = db.Column(db.Integer, db.ForeignKey("tb_course.id"), primary_key=True, doc='课程id')
    tag_id = db.Column(db.Integer, db.ForeignKey("tb_course_tag.id"), primary_key=True, doc='标签id')
    is_delete = db.Column(db.Boolean, doc='状态(0存在对应关系;1不存在对应关系)')


class Chapters(Base):
    """
    章节目录
    """
    __tablename__ = 'tb_chapters'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(24), doc='章标题')
    serial_num = db.Column(db.Integer, doc='章序号')
    course = db.Column(db.Integer, db.ForeignKey("tb_course.id", ondelete="CASCADE"), doc='课程')
    sections = db.relationship('Sections', backref='tb_chapters')

    def __str__(self):
        return self.title


class Sections(Base):
    """
    小节
    """
    __tablename__ = 'tb_sections'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(24), doc='章标题')
    serial_num = db.Column(db.Integer, doc='章序号')
    chapters = db.Column(db.Integer, db.ForeignKey("tb_chapters.id", ondelete="CASCADE"), doc='课程')
    learn_time = db.Column(db.Integer, doc='学习小时', default=1)
    content = db.Column(db.Text, doc='学习内容')
    video = db.Column(db.String(256), doc='学习视频')
    seq_num = db.Column(db.Integer, doc='序号', default=1)
    like_count = db.Column(db.Integer, doc='点赞次数', default=0)

    def __str__(self):
        return self.title

class Comment(db.Model):
    __tablename__ = 'tb_comment'
    cid = db.Column(db.Integer,primary_key=True,autoincrement=True)
    course_id = db.Column(db.Integer,db.ForeignKey('tb_course.id',ondelete='CASCADE'))
    user_id = db.Column(db.ForeignKey('tb_users.uid',ondelete='CASCADE'))
    parent_id = db.Column(db.ForeignKey('tb_comment.cid',ondelete='CASCADE'),doc='上级评论')
    content = db.Column(db.String(1000),doc='内容')
    created_time = db.Column(db.DateTime,default=datetime.datetime.now(),doc='评论时间')
    # to_user = db.Column(db.ForeignKey('tb_users.uid',ondelete='CASCADE'),nullable=True,doc='回复评论的用户')
    like_count = db.Column(db.Integer, default=0, doc='点赞数')
    reply_count = db.Column(db.Integer, default=0, doc='回复数')